#ifndef SUBWINDOW_H
#define SUBWINDOW_H

#include <stddef.h>
//#include <raygui.h>

typedef struct SWindowState
{
	bool windowActive;
	Rectangle windowBounds;
	Vector2 panOffset;
	int contentHeight;
	Vector2 scrollPanelOffset;
} SWindowState;

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

int swinLineHeight = 24, swinLineHeightBlank = 12;

SWindowState initSWinState(int scnWidth, int scnHeight, int width, const char *lines[], int count)
{
	SWindowState state = { 0 };

	state.windowActive = false;

	state.windowBounds = (Rectangle) { (scnWidth - width) / 2.f, 0, width, 0 };
	state.panOffset = (Vector2) { 0, 0 };

	// Calculate content height
	state.contentHeight = 0;
	for (int i = 0; i < count; i++)
	{
		if (lines[i] == NULL)
			break;
		else if ((lines[i][0] == '-') && (lines[i][1] == '\0'))
			state.contentHeight += swinLineHeightBlank;
		else
			state.contentHeight += swinLineHeight;
	}
	state.contentHeight += 8;    // Marging at the end

	// Calculate window height if not specifically defined
	if (state.windowBounds.height == 0)
		state.windowBounds.height = state.contentHeight + 24 + 4;
	state.windowBounds.y = (scnHeight - state.windowBounds.height) / 2;

	// Review size if it does not fit on the screen
	if (state.windowBounds.height > (scnHeight - 80))
	{
		state.windowBounds.height = scnHeight - 80;
		state.windowBounds.y = (scnHeight - state.windowBounds.height) / 2;
	}

	// Init scroll offset
	state.scrollPanelOffset = (Vector2) { 0, 0 };

	return state;
}

void drawSWindow(SWindowState *state, int scnWidth, int scnHeight, const char *title, const char *lines[], int count)
{
	if (state->windowActive)
	{
		// Update window bounds and size: center to screen and allow scrolling
		//----------------------------------------------------------------------------------------
		state->windowBounds.x = (scnWidth - state->windowBounds.width) / 2;
		state->windowBounds.height = state->contentHeight + 24 + 4;
		state->windowBounds.y = (scnHeight - state->windowBounds.height) / 2;

		// Review size if it does not fit on the screen
		if (state->windowBounds.height > (scnHeight - 80))
		{
			state->windowBounds.height = scnHeight - 80;
			state->windowBounds.y = (scnHeight - state->windowBounds.height) / 2;
		}
		//----------------------------------------------------------------------------------------

		// Draw window and controls
		//----------------------------------------------------------------------------------------
		state->windowActive = !GuiWindowBox(state->windowBounds, title);

		int nextLineY = swinLineHeight + 4;

		// Draw scroll panel considering window bounds and content size (only if required)
		Rectangle scissor = { 0 };
		if (state->contentHeight > (state->windowBounds.height - 24))
		{
			GuiScrollPanel((Rectangle) { state->windowBounds.x, state->windowBounds.y + swinLineHeight - 1, state->windowBounds.width, state->windowBounds.height - 24 + 1 },
			NULL,
			(Rectangle) { state->windowBounds.x, state->windowBounds.y + swinLineHeight, state->windowBounds.width - 16, (float)state->contentHeight },
			&state->scrollPanelOffset,
			&scissor);
		}

		// Draw help info and separation lines
		// WARNING: We only scissor if scrolling is required, scissor mode forces a new draw call
		if (state->contentHeight > (state->windowBounds.height - 24))
			BeginScissorMode((int)scissor.x, (int)scissor.y, (int)scissor.width + 2, (int)scissor.height);

		for (int i = 0; i < count; i++)
		{
			if (lines[i] == NULL)
				break;
			else if ((lines[i][0] == '-') && (lines[i][1] == '\0'))
				GuiLine((Rectangle) { state->windowBounds.x, state->windowBounds.y + nextLineY + state->scrollPanelOffset.y, state->windowBounds.width, swinLineHeightBlank }, NULL);
			else if (lines[i][0] == '-')
				GuiLine((Rectangle) { state->windowBounds.x, state->windowBounds.y + nextLineY + state->scrollPanelOffset.y, state->windowBounds.width, swinLineHeight }, lines[i] + 1);
			else
				GuiLabel((Rectangle) { state->windowBounds.x + 12, state->windowBounds.y + nextLineY + state->scrollPanelOffset.y, state->windowBounds.width, swinLineHeight }, lines[i]);

			if ((lines[i][0] == '-') && (lines[i][1] == '\0'))
				nextLineY += swinLineHeightBlank;
			else
				nextLineY += swinLineHeight;
		}

		if (state->contentHeight > (state->windowBounds.height - 24))
			EndScissorMode();
	}
}

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // SUBWINDOW_H